package com.rd.student.jike.class2.xuezhixin.day0807;

import java.io.*;
import java.util.LinkedList;

public class Work4_8 {
    public static void main(String[] args) throws Exception{
        winningNum();
        Lottery();
        compare();
        File file = new File("daletou.txt");
        File file1 = new File("randomDLT.txt");
    }

    public static void winningNum() throws Exception{
        File file = new File("daletou.txt");
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 0;i<7;i++){
            if (i<5){
                int num = (int)(Math.random()*35 + 1);
                if (list.contains(num)){
                    i--;
                    continue;
                }
                list.add(num);
            }else {
                int num = (int)(Math.random()*12 + 1);
                if (list.contains(num)){
                    i--;
                    continue;
                }
                list.add(num);
            }
        }


        Writer writer = new FileWriter(file);
        StringBuilder builder = new StringBuilder();
        for (int i = 0;i<list.size();i++){
            if (i == 5){
                builder.append("| ");
            }
            if (i == list.size()-1){
                builder.append(list.get(i));
                break;
            }
            builder.append(list.get(i) + " ");
        }

        writer.write(builder.toString());
        writer.close();

        System.out.println("中奖号码已出");
    }

    public static void Lottery() throws Exception{
        File file = new File("randomDLT.txt");
        StringBuilder builder = new StringBuilder();
        int quantity = 1;
        while (true){
            LinkedList<Integer> list = new LinkedList<>();
            for (int i = 0;i<7;i++){
                if (i<5){
                    int num = (int)(Math.random()*35 + 1);
                    if (list.contains(num)){
                        i--;
                        continue;
                    }
                    list.add(num);
                }else {
                    int num = (int)(Math.random()*12 + 1);
                    if (list.contains(num)){
                        i--;
                        continue;
                    }
                    list.add(num);
                }
            }

            for (int i = 0;i<list.size();i++){
                if (i == 5){
                    builder.append("| ");
                }
                if (i == list.size()-1){
                    builder.append(list.get(i));
                    break;
                }
                builder.append(list.get(i) + " ");
            }
            builder.append("\n");
            if (quantity++ == 100){
                break;
            }
        }
        Writer writer = new FileWriter(file);
        writer.write(builder.toString());
        System.out.println("彩票已经选购完毕");
        writer.close();
    }

    public static void compare() throws Exception{
        File fileWin = new File("daletou.txt");
        File file = new File("randomDLT.txt");

        Reader reader1 = new FileReader(fileWin);
        Reader reader2 = new FileReader(file);

        char[] chars = new char[(int)file.length()];
        char[] charsWin = new char[(int) fileWin.length()];
        reader1.read(charsWin);
        reader2.read(chars);
        String strWin = new String(charsWin);
        String strChar = new String(chars);
        String[] str = strChar.split("\n");
        reader1.close();
        reader2.close();
        System.out.println("中奖号码为：");
        System.out.println(strWin + "\n");

        for (String strLottery : str){
            System.out.println(strLottery);
            if (strLottery.equals(strWin)){
                System.out.println("恭喜中奖！！！！");
                return;
            }
        }
        System.out.println("很遗憾您没有中奖");


    }

}
